package com.aidex.biz.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

/**
 * 云象优客 加密
 */
public class YunXiangYouKeAESUtils {
    public static final String AES_CBC_PKCS5PADDING = "AES/CBC/PKCS5PADDING";
    public static final String KEY = "9a298c0f4f094c8ab08fe1baa3c91b0e";

    public static final String IV = "tydf18129979469s";

    public static final String APPID = "e85b092acaee43bc959411463d2cf43a";
    public static final String APPKEY = "2520f107d4724bda93d972a588b04966";


    /**
     * key为16位
     *
     * @param key * @param value * @return
     */
    public static String encrypt(String key, String value) {
        return encrypt(key, key, value);
    }

    public static String encrypt(String key, String initVector, String value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(StandardCharsets.UTF_8));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(value.getBytes());
            return Base64.encodeBase64String(encrypted);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * @param key
     * @param encrypted
     * @return
     */
    public static String decrypt(String key, String encrypted) {
        return decrypt(key, key, encrypted);
    }


    public static String decrypt(String key, String initVector, String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(StandardCharsets.UTF_8));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
            return new String(original);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }




    public static void main(String[] args) {
        System.out.println(YunXiangYouKeAESUtils.decrypt(KEY, IV, "lL9WOa7V9FHf+R/+7JdhEhQZrjDnRX+Hs6jGYkOWOx15aYjFOjt9c3zk5S2xl34QlEBOt7u\n" + "YiZWUsDEZGSd4nFwKrc1xXuLywxwilY0HzVQjnaPPOF8aHosGPw7n2ZLtgWIrZFT4vJk7TT\n" + "bwsODmwQ1VAJH56ImWSLLWaNt1Eo0FO5c67iCrQr5tBIcm6DkzeUuMBXawc4J0QrCy+PVIh\n" + "x8I+Yks6uuAcbT4ghZfIrHGxx4jDjd4jjLteC4b2Y/xDrGzcD7td7AljZU6crc08I+zGsBW\n" + "nhBVRIRjDcCrhBIR5WL7R6N7u10sKIXOpHX6BbfqwL7Y0H/rODwf5VYA5M55lpddDTkOjPQ\n" + "X+HCXtuKgAEtzrbRCozw5gpqMLxiPvIub58REuieXOl7fMSvZfK07AFeTz3n5jSXW9Bg8UZ\n" + "jFUaK9xurVbfxXzSoovj2/15F7LEDVox/MG4aOZDSu29l9N6PLE+okpmyVKSa7r2KzhBVlS\n" + "VYJF/XBP/ptEZao"));

    }





}